package com.iyao_app.adapter;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.iyao_app.R;
import com.iyao_app.entry.SignMapData;
import com.iyao_app.entry.StoryDetail;
import com.iyao_app.util.CountUtils;
import com.iyao_app.util.MarkUtil;
import com.iyao_app.view.OnImageClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.github.rockerhieu.emojicon.EmojiconTextView;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/12/16.
 */

public class SignMapAdapter extends BaseExpandableListAdapter {
    private List<SignMapData> mData = new ArrayList<>();

    public SignMapAdapter(List<SignMapData> dataList) {
        if (dataList != null) {
            mData.addAll(dataList);
        }
    }

    @Override
    public int getGroupCount() {
        return mData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (mData == null
                || mData.size() == 0
                || mData.get(groupPosition) == null
                || mData.get(groupPosition).list == null
                || mData.get(groupPosition).list.size() == 0) {
            return 0;
        } else {
            return mData.get(groupPosition).list.size();
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mData.get(groupPosition).list.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group, null);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.mTvTime.setText(mData.get(groupPosition).time);
        groupViewHolder.mIvXiala.setVisibility(View.GONE);
//        groupViewHolder.mTvWeek.setText(String.format("星期%s", TimeUtils.getChineseWeek(item.createTime)));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_child, null);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        StoryDetail item = mData.get(groupPosition).list.get(childPosition);
        childViewHolder.mSdvHead.setImageResource(MarkUtil.getId(item.typecode));


        childViewHolder.mTvName.setText(item.nickname);
        childViewHolder.mTvLocation.setText(item.sceneName);
        childViewHolder.mTvLike.setText(String.format("赞 (%s)", CountUtils.getCount(item.goodCount)));

        if (TextUtils.isEmpty(item.eventDesc)) {
            childViewHolder.mLinV2.setVisibility(View.GONE);
        } else {
            childViewHolder.mLinV2.setVisibility(View.VISIBLE);
            childViewHolder.mTvContent.setText(item.eventDesc);
        }
        if (item.eventFileUrlList != null && item.eventFileUrlList.size() > 0) {
            childViewHolder.mLinV3.setVisibility(View.VISIBLE);
            StaggeredGridLayoutManager layoutManager = null;
            OnlyPictureAdapter adapter = null;
            if (item.eventFileUrlList.size() == 0) {
                layoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
                adapter = new OnlyPictureAdapter(item.eventFileUrlList);
            } else {
                layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
                adapter = new OnlyPictureAdapter(item.eventFileUrlList);
            }
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    List<String> data = adapter.getData();
                    if (imageClickListener!=null) {
                        imageClickListener.onImageClick(data,position);
                    }


                }
            });

            childViewHolder.mRecyclerView.setAdapter(adapter);
            childViewHolder.mRecyclerView.setLayoutManager(layoutManager);

        } else {
            childViewHolder.mLinV3.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(item.eventDesc) && item.eventFileUrlList == null || item.eventFileUrlList.size() == 0) {
            childViewHolder.mV1.setVisibility(View.VISIBLE);
        } else {
            childViewHolder.mV1.setVisibility(View.GONE);
        }


        return convertView;
    }

    OnImageClickListener imageClickListener;

    public void setOnImageClickListener(OnImageClickListener onImageClickListener) {
        imageClickListener = onImageClickListener;
    }

    public void addData(List<SignMapData> data) {
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public List<SignMapData> getData() {
        return mData;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    static class GroupViewHolder {
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.iv_xiala)
        ImageView mIvXiala;
        @BindView(R.id.lin_time)
        LinearLayout mLinTime;

        GroupViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ChildViewHolder {
        @BindView(R.id.v_line)
        View mVLine;
        @BindView(R.id.sdv_head)
        SimpleDraweeView mSdvHead;
        @BindView(R.id.tv_name)
        EmojiconTextView mTvName;
        @BindView(R.id.iv_tag)
        ImageView mIvTag;
        @BindView(R.id.tv_location)
        TextView mTvLocation;
        @BindView(R.id.iv_like)
        ImageView mIvLike;
        @BindView(R.id.tv_like)
        TextView mTvLike;
        @BindView(R.id.lin_v_1)
        LinearLayout mLinV1;
        @BindView(R.id.tv_content)
        EmojiconTextView mTvContent;
        @BindView(R.id.lin_v_2)
        LinearLayout mLinV2;
        @BindView(R.id.recyclerView)
        RecyclerView mRecyclerView;
        @BindView(R.id.lin_v_3)
        LinearLayout mLinV3;
        @BindView(R.id.rl_root)
        LinearLayout mRlRoot;
        @BindView(R.id.v_1)
        View mV1;


        ChildViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
